from django.db import models
# Create your models here.

class Project(models.Model):
    """项目表"""
    name = models.CharField(max_length=128,blank=False,null=False,verbose_name="项目名称")
    name_en = models.CharField(max_length=64,blank=False,null=False,verbose_name="项目目录名")
    describe = models.CharField(max_length=512,blank=True,null=True,verbose_name="项目描述")
    cloud_project_id = models.CharField(max_length=128,blank=False,null=True,verbose_name="云项目id1")
    cloud_account_id1 = models.CharField(max_length=128,blank=False,null=True,verbose_name="云项目id1关联的云账号")
    cloud_project_id2 = models.CharField(max_length=128, blank=False, null=True, verbose_name="云项目id2")
    cloud_account_id2 = models.CharField(max_length=128, blank=False, null=True, verbose_name="云项目id2关联的云账号")
    cloud_project_id3 = models.CharField(max_length=128, blank=False, null=True, verbose_name="云项目id3")
    cloud_account_id3 = models.CharField(max_length=128, blank=False, null=True, verbose_name="云项目id3关联的云账号")
    create_time= models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "项目表"
        db_table = "project"

class SelectDefaultProject(models.Model):
    """自动选择上次选中项目"""
    select_project_id = models.IntegerField(blank=True,null=False,verbose_name="已选择的项目id")
    b_user = models.ForeignKey("User", on_delete=models.CASCADE, verbose_name="属于哪个用户")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.select_project_id

    class Meta:
        verbose_name_plural = "自动选择上次选中项目"
        db_table = "selectdefaultproject"

class Cloud_Account(models.Model):
    """公有云账号表"""
    platform = models.CharField(max_length=128,blank=False,null=False,verbose_name="云平台")
    account = models.CharField(max_length=128,blank=False,null=False,verbose_name="账号")
    name = models.CharField(max_length=64,blank=False,null=False,verbose_name="名称")
    appid = models.CharField(max_length=512,blank=True,null=True,verbose_name="appid")
    secretId = models.CharField(max_length=128,blank=False,null=True,verbose_name="密钥id")
    secretKey = models.CharField(max_length=128, blank=False, null=True, verbose_name="密钥key")
    create_time= models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.account

    class Meta:
        verbose_name_plural = "公有云账号表"
        db_table = "cloud_account"

class UserType(models.Model):
    """用户类型表"""
    name = models.CharField(max_length=64,blank=False,null=False,verbose_name="用户类型")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "用户类型表"
        db_table = "usertype"

class MyAuthType(models.Model):
    """认证类型"""
    auth_type = models.CharField(max_length=32,blank=False,null=False,verbose_name="认证类型")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.auth_type

    class Meta:
        verbose_name_plural = "认证类型"
        db_table = "myauthtype"

class User(models.Model):
    """用户表"""
    name = models.CharField(max_length=64,blank=False,null=False,verbose_name="用户名")
    password = models.CharField(max_length=64,blank=False,null=False,verbose_name="密码")
    usertype = models.ForeignKey("UserType",on_delete=models.CASCADE,verbose_name="用户类型")
    phone = models.CharField(max_length=64,blank=True,null=True,verbose_name="联系号码")
    phone_bak = models.CharField(max_length=64, blank=True, null=True, verbose_name="备用联系号码")
    project = models.ManyToManyField("Project",verbose_name="用户负责的项目")
    motto = models.CharField(max_length=512,blank=True,null=True,verbose_name="座右铭")
    hobby = models.CharField(max_length=512,blank=True,null=True,verbose_name="兴趣爱好")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "用户表"
        db_table = "user"

class ServerHost(models.Model):
    """机器表"""
    host_name = models.CharField(max_length=64,blank=True,null=True,verbose_name="名字")
    host_ip = models.CharField(max_length=64,blank=True,null=True,verbose_name="ip地址")
    host_private_ip = models.CharField(max_length=64, blank=True, null=True, verbose_name="内网ip地址")
    host_port = models.IntegerField(default=22,verbose_name="ssh端口")
    b_project = models.ForeignKey("Project",on_delete=models.CASCADE,verbose_name="属于哪个项目")
    config_size = models.CharField(max_length=128,blank=True,null=True,verbose_name="配置大小")
    os_system = models.CharField(max_length=128,blank=True,null=True,verbose_name="操作系统")
    instance_id = models.CharField(max_length=64,blank=True,null=True,verbose_name="云实例id")
    instances_status = models.CharField(max_length=64,blank=True,null=True,verbose_name="运行状态")
    instances_region = models.CharField(max_length=64,blank=True,null=True,verbose_name="所在地区")
    cloud_account = models.CharField(max_length=128,blank=True,null=True,verbose_name="关联云账号")
    cloud_platform = models.CharField(max_length=128, blank=True, null=True, verbose_name="关联云平台")
    user = models.ManyToManyField("User",verbose_name="属于哪个用户")     #就不应该让它存在，让项目来控制就好
    init_status = models.CharField(max_length=32,blank=True,null=False,default=False,verbose_name="是否初始化")
    server_purpose_choices = (
        (1, "游戏服务器"),
        (2, "其他")
    )
    server_purpose = models.SmallIntegerField(choices=server_purpose_choices, default=1, verbose_name="服务器用途")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.host_ip

    class Meta:
        verbose_name_plural = "机器表"
        db_table = "serverhost"

class DomainList(models.Model):
    """域名解析表"""
    complete_domain = models.CharField(max_length=128,blank=True,null=False,verbose_name="域名")
    domain = models.CharField(max_length=128,blank=True,null=False,verbose_name="一级域名")
    sub_domain = models.CharField(max_length=128,blank=True,null=False,verbose_name="主机记录值")
    ip_address = models.CharField(max_length=128,blank=True,null=False,verbose_name="ip地址")
    b_project = models.ForeignKey("Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    record_id = models.CharField(max_length=128,blank=True,null=False,default="0",verbose_name="记录唯一id")
    record_line = models.CharField(max_length=128,blank=True,null=False,default="默认",verbose_name="线路类型")
    record_type = models.CharField(max_length=128,blank=True,null=False,verbose_name="记录类型")
    ttl = models.IntegerField(default=600,verbose_name="缓存生存时间")
    status = models.CharField(max_length=128,blank=True,null=False,default="enable",verbose_name="记录状态")    #记录初始状态，默认为”enable”，如果传入”disable”，解析不会生效，也不会验证负载均衡的限制。
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.domain

    class Meta:
        verbose_name_plural = "域名解析表"
        db_table = "domainlist"

class GameServerGroup(models.Model):
    """游戏分区表"""
    group_name = models.CharField(max_length=64,blank=True,null=False,verbose_name="游戏分区")
    gameserver_number = models.IntegerField(blank=True,null=True,verbose_name="游戏数量")
    gameserver_number_not_son = models.IntegerField(blank=True,null=True,verbose_name="母服数量")
    gameserver_number_son = models.IntegerField(blank=True,null=True,verbose_name="子服数量")
    b_project = models.ForeignKey("Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.group_name

    class Meta:
        verbose_name_plural = "游戏分区表"
        db_table = "gameservergroup"

class OpsLogger(models.Model):
    """操作记录表"""
    log_user = models.CharField(max_length=32,blank=True,null=False,verbose_name="操作人")
    log_time = models.DateTimeField(auto_now_add=True,verbose_name="操作时间")
    log_ops = models.CharField(max_length=512,blank=True,null=False,verbose_name="操作内容")

    def __str__(self):
        return self.log_ops

    class Meta:
        verbose_name_plural = "操作记录表"
        db_table = "opslogger"

class AuditLog(models.Model):
    """审计日志表"""
    op_content = models.CharField(max_length=512,blank=True,null=True,verbose_name="审计内容")
    op_host = models.OneToOneField("ServerHost",on_delete=models.CASCADE,verbose_name="操作关联机器")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.op_host

    class Meta:
        verbose_name_plural = "审计日志表"
        db_table = "auditlog"

class DevRecord(models.Model):
    """开发记录表"""
    content = models.CharField(max_length=512,blank=True,null=True,verbose_name="开发内容")
    name = models.CharField(max_length=32,blank=True,null=True,verbose_name="开发人")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="开发时间")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name_plural = "开发记录表"
        db_table = "devrecord"

class Document(models.Model):
    """运维文档表"""
    title = models.CharField(max_length=512,blank=True,null=False,verbose_name="标题")
    content = models.TextField(blank=True,null=True,verbose_name="内容")
    b_user = models.ForeignKey("User", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="属于哪个用户")
    b_project = models.ForeignKey("Project",on_delete=models.CASCADE,verbose_name="属于哪个项目")
    see_range = models.CharField(max_length=128,blank=True,null=True,verbose_name="可见范围")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "运维文档表"
        db_table = "document"

class SelectTheme(models.Model):
    """主题"""
    theme_number = models.IntegerField(default=0,verbose_name="ssh端口")
    user = models.ForeignKey("User", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="所属用户")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.theme_number

    class Meta:
        verbose_name_plural = "主题"
        db_table = "selecttheme"
